<?php ob_start(); ?> 
<?php header ('Content-type: text/xml; charset=utf-8'); ?>     

<?php include "constants.inc" ?>

<?php include "classes.inc" ?>

<?php session_start(); ?>

<?php

function storePlayerTeam() {

	

	if (isset($_GET['teamNr'])) {

		$teamNr = $_GET['teamNr'];

	}	

	if (isset($_GET['playerListId'])) {

		$playerListId = $_GET['playerListId'];

	}	

	if (isset($_GET['clubId'])) {

		$clubId = $_GET['clubId'];

	}	

	if (isset($_GET['row'])) {

		$row = $_GET['row'];

	}

	

	if (isset($playerListId)) {

		if (isset($_SESSION["players".$playerListId])) {

			$players = $_SESSION["players".$playerListId];

		}

    }

    

    if (!isset($players) & isset($_SESSION["players"])) {

    	$players = $_SESSION["players"];

    }



    if (empty($clubId)) {

    	$clubId = $_SESSION['user']->club_id;

    }



	if ( isset($players) && isset($teamNr) && isset($row) && !empty($clubId) ) {

        $player = $players[$row-1];



        $connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);

		mysql_select_db(DB_CATALOG);

		$result = mysql_query("select count(1) as nrOfTeams from zt_team te

			join zt_tournament t on t.tournament_id = te.tournament_id

			where t.iscurrent = 1 and club_id = ".$clubId);

        $record = mysql_fetch_assoc($result);

        $nrOfTeams = $record['nrOfTeams'];

        if (is_numeric($teamNr) == true) {

            if ($nrOfTeams >= $teamNr) {

                // team number is valid

            	$player->team_name = $teamNr;

	            $players[$row-1] = $player;

	            if (isset($playerListId)) {

                    $_SESSION["players".$playerListId] = $players;

                    //return "debug by webmaster, please ignore: fout -> ".$teamNr." + ".$playerListId;

                } else {

                    $_SESSION["players"] = $players;

                    //return "debug by webmaster, please ignore: ok -> ".$teamNr." + ".$players[$row-1]->team_name;

                }

	        } else {

                // team number is not valid

	            return 'Ploegnummer '.$teamNr." is niet geldig (max. ".$nrOfTeams." ploegen toegelaten).";

	        }

        } else if (empty($teamNr)) {

            $player->team_name = $teamNr;

	        $players[$row-1] = $player;

            if (isset($playerListId)) {

            	$_SESSION["players".$playerListId] = $players;

            	//return "debug by webmaster, please ignore: fout".$playerListId;

            } else {

                $_SESSION["players"] = $players;

                //return "debug by webmaster, please ignore: ok";

            }

        }



	    return '1';

    }



    return 'Ploegnummer niet opgeslagen (ploegnummer='.$teamNr.", rijnummer=".$row.", club=".$clubId."). Verwittig de webmaster aub.";

}

?>

<response>

  <method>storePlayerTeam</method>

  <result><?php echo storePlayerTeam()?></result>

</response>